$(function () {
	//脱敏日志
	//----数据库动态脱敏
	/**
     * Init
     *
     * 
     * |-- 
     * |   `-- 
     * |   `-- 
     * |	   `-- 
     * |		   `-- 
     * |		   `-- 
     * |			   `-- 
     * |		   `-- 
     * |-- 
     * |-- 
     */
	var ad = appData();
	
	var tmplObj = null;
	var arry1 = [],arry2 = [],arry3 = [];
	console.log('已加载')
	
	DomNameStart()
	function DomNameStart() {
		
		  $("#search2").on("click",function(){
		    $(".advanced-search").toggle(200);
		  })  
		//侧面添加class、索引加载
		//$('.sys-sidebar ul li').eq(0).find('ul li ').eq(0).addClass('active')
		
		var name = $('.sys-sidebar').find('li .active a').text()
		
		$("#TopName").text(name)//替换头部日志名称
		loadDom('POOL_AD5',"#tmName",'')

	
		
	
		
		
		
		

		function loadDom(api,id,val) {
			ad.selectList(api, {},function(data) {
				if(val==''){
					$(id).empty()
				}else{
					$(id).empty().append("<option value='-1'> "+val+"</option>")
				}
				for(var i=0;i<data.length;i++){
					$(id).append("<option value='"+data[i].id+"'>"+data[i].value+"</option>")
				}
				
				
			})
		}
		
		ad.selectList("AUDIT_TTYPE5",{},function(data) {
			$("#audiTtype").empty()
			for(var i=0;i<data.length;i++){
				$("#audiTtype").append("<li><a href='javascript:void(0);'>"+data[i].value+"</a></li>")
			}
			ad.selectList("VISIT_USER",{},function(data) {
				$("#visitUser").empty()
				for(var i=0;i<data.length;i++){
					$("#visitUser").append("<li><a href='javascript:void(0);'>"+data[i].value+"</a></li>")
				}
				
				showTable(true, 1)
				initTime("#DateRange")
				$("#DateRange").attr('readonly','readonly')
			})
			
		})
		
		
		
	}
	
	domCase()
	function domCase() {
		$("#search1").on('click',function(){
			var searchVal  = $("#searchVal").val() 
			
			showTable(true,1)
		})		
		//arry1 arry2 arry3
		//poolType  audiTtype ConType2
		
		addDomName("#poolType",arry1),addDomName("#audiTtype",arry2),addDomName("#visitUser",arry3)
		
		
		function addDomName(id,arrys) {
			 $(id).on('click','li',function(){
				var _this = $(this);
				if(_this.hasClass('active')){
					_this.removeClass('active')
				}else{
					_this.addClass('active')
				}
				if(arrys.indexOf(_this.text())==-1){
					arrys.push(_this.text())
				}else{
					var num =  arrys.indexOf(_this.text());
	    				arrys.splice(num,1);
				}
				console.log(arrys)
			})
		}
		
	}
	//表格show加载
	function showTable(dontRenderPager, page) {
		var top1 = {
			
			"dateRange":$("#DateRange").val(),//时间范围 
			"keyType":$("#tmName").val()==""? -1 :$("#tmName").val(),//来源设备名称	
			"keyValue":$("#searchVal").val()//查询关键词
		}
		var top2 = {
			
			"poolType":arry1,
			"audiTtype":arry2,
			
			"visitUser":arry3//访问专户
			
			
		}
		var params = {
			"page":page,
			"top1":top1,
			"top2":top2,
			"pageSize": appApi('PAGE_SIZE')
		}
		
		ad.insert("TM_LIST2", params, function(data) {
			
			$("#tableBody").empty();
			if (data.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}
		
			
			$("#tableBody").empty().append($.tmpl(tmplObj, data.content));
			if (dontRenderPager) {
				renderPager(data.totalRecord);
			}
		})
	}
	
	function renderPager(totalRecord, nowPage) {
		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {
			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
			}
		}, nowPage);
	};
	
	
	//数据的下载
	$("#loadLog").on("click",function(){
		
		uploadingFile()
		
	})
   
	//数据载入按钮
	function uploadingFile() {
		
		
		var types = $("#TopName").text()
		console.log(types)
		//var param = "key=222";
		
		
		$.downloadInterface($.getRadParamUrl(appApi("T_UPLOADING_FILE5")),"get",{name:types});
		
	};
	
	//时间插件加载
	function initTime(id){
    	$(id).daterangepicker({
			singleDatePicker:false,
            showDropdowns: true,
            timePicker12Hour: false,
            startDate: moment().startOf('day'),
            format: 'YYYY-MM-DD'
   		});
   		$('.daterangepicker').width('512px')
    }
	
	
	tmplObj = `<tr>
              
                <td><a href="javascript:void(0);">{{= tmId}}</a></td>
                <td>{{= tmObj}}</td>
                <td>{{= ip}}</td>                
                <td>{{= idLocal}}</td>
                <td>{{= dataType}}</td>
                <td>{{= userIp}}</td>
                 <td>&nbsp;</td>
                <td><span class="badge badge-success {{if result!="成功"}}badge-danger{{/if}}">{{= result}}</span></td>
                <td>{{= time}}</td>
              </tr>`;
              
//			<td><a href="javascript:void(0);">1708220005</a></td>
//              <td>db-people-info</td>
//              <td>182.22.176.24</td>
//              <td>182.22.176.24</td>
//              <td>个人信息</td>
//              <td>182.22.176.24</td>
//              <td>&nbsp;</td>
//              <td><span class="badge badge-success">成功</span></td>
//          <td>2017-08-25 09:20:30</td>
})